﻿using System;
using System.Collections.Generic;
using Cos.Core.Model.Common;
using Cos.Core.Model.Form;
using Cos.Module.Form.Data;
using Cos.Module.Form.Exceptions;

namespace Cos.Module.Form
{
    public class FormService : IFormService
    {
        private readonly IFormRepository _formRepository;

        public FormService(IFormRepository formRepository)
        {
            this._formRepository = formRepository;
        }

        /// <summary>
        /// Get the form from form id
        /// </summary>
        /// <param name="formId">the id of the form</param>
        /// <param name="culture">the culture vo</param>
        /// <param name="isLiveMode">true if the user is viewing the site in live mode</param>
        /// <returns>a form</returns>
        public Core.Model.Form.Form GetForm(int formId, Culture culture, bool isLiveMode)
        {
            // Check for invalid parameters
            if (formId <= 0)
                throw new FormNotFoundException("Not such form in the database. Form.ID: " + formId);
            
            if (culture == null)
                throw new FormNotFoundException("Not such form in the database. Culture: " + culture);
            
            return _formRepository.GetForm(formId, culture, isLiveMode);
        }

        /// <summary>
        /// Get the form from form id
        /// </summary>
        /// <param name="formId">the id of the form</param>
        /// <param name="cultureCode">the culture id</param>
        /// <param name="isLiveMode">true if the user is viewing the site in live mode</param>
        /// <returns>a form</returns>
        public Core.Model.Form.Form GetForm(int formId, string cultureCode, bool isLiveMode)
        {
            // Check for invalid parameters
            if (formId <= 0)
                throw new FormNotFoundException("Not such form in the database. Form.ID: " + formId);
            
            if (cultureCode == null)
                throw new FormNotFoundException("Not such form in the database. Culture: " + cultureCode);
            
            return _formRepository.GetForm(formId, cultureCode, isLiveMode);
        }

        /// <summary>
        /// Used for polls. Get the count of for each element choice for a form element
        /// </summary>
        /// <param name="formElement">The form element we want the count for</param>
        /// <returns>A dictionary of [element choice value, count]</returns>
        public IDictionary<string, int> GetAnswerCount(int formElement)
        {
            return _formRepository.GetAnswerCount(formElement);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="submission"></param>
        /// <param name="isLiveMode"></param>
        /// <param name="generator"></param>
        public void SubmitForm(BasicCameleonFormSubmission submission, bool isLiveMode, string generator)
        {
            // Does the submission have all the required information
            if (submission == null)
                throw new NullReferenceException("Submission cannot be null.");
            
            if (submission.CultureCode == null)
                throw new NullReferenceException("Submission cannot have a null culture code.");

            if (submission.SubmissionDate == null)
            {
                submission.SubmissionDate = DateTime.Now;
            }
        }
    }
}